package cn.schoolwow.workflow.test.module.analyzeWorkReport.query.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceQuery;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.module.common.domain.WorkFlowTaskType;
import org.junit.Assert;

import java.util.Arrays;

public class QueryAuditTaskByCurrentUserTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkStartByMe(flowContext);
        checkAuditByMe(flowContext);
        checkJoinByMe(flowContext);
        checkCarbonCopyToMe(flowContext);
    }

    @Override
    public String name() {
        return "查询当前用户审批任务";
    }

    private void checkStartByMe(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);

        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.initiatorList.add(startWorkFlowRequest.initiator);

        long count = quickWorkFlow.getWorkFlowInstanceCount(workFlowInstanceQuery);
        Assert.assertEquals(1,count);
    }

    private void checkAuditByMe(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
        workFlowTaskQuery.assignerList = Arrays.asList(startWorkFlowRequest.contextData.getString("firstAuditUserId"));
        workFlowTaskQuery.complete = false;
        workFlowTaskQuery.instanceStatusList = Arrays.asList(WorkFlowInstanceStatus.Running.status);

        long count = quickWorkFlow.getWorkFlowTaskCount(workFlowTaskQuery);
        Assert.assertEquals(1,count);
    }

    private void checkJoinByMe(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);

        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.assignerList.add(startWorkFlowRequest.contextData.getString("firstAuditUserId"));
        workFlowInstanceQuery.taskTypeList.add(WorkFlowTaskType.AuditTask.value);

        long count = quickWorkFlow.getWorkFlowInstanceCount(workFlowInstanceQuery);
        Assert.assertEquals(1,count);
    }

    private void checkCarbonCopyToMe(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        StartWorkFlowRequest startWorkFlowRequest = flowContext.checkInstanceData(StartWorkFlowRequest.class);

        WorkFlowInstanceQuery workFlowInstanceQuery = new WorkFlowInstanceQuery();
        workFlowInstanceQuery.carbonCopyList.addAll(startWorkFlowRequest.carbonCopyList);

        long count = quickWorkFlow.getWorkFlowInstanceCount(workFlowInstanceQuery);
        Assert.assertEquals(1,count);
    }
}
